// pages/search/search.js
var app = getApp(); //获取应用实例
const checkUtils = require('../../utils/formatCheckUtils.js'); //格式校验模块引入
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabList: [],
    isSeclctMode: false,
    selectTotal: 0
  },
  getData: function(keyword = '') {
    var params = {};// companyId: 123123 
    if (keyword != ''){
      params.keyword = keyword;
    }
    var that = this;
    app.requestAPI.findAllByCompanyId(params)
      .then(d => {
        console.log(d)
        if (d.code == 200) {
          for (var item in d.data) {
            d.data[item].isSelect = false;
          }

          that.setData({
            tabList: d.data
          })
        } else {
          that.setData({
            tabList: []
          })
        }
      }).catch(e => {
        console.log(e)
        app.tip.toast(e.errMsg)
        app.tip.log('getIndex请求异常')
      });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getData();
  },
  selectTab: function(e) {
    console.log(e)
    if (this.data.tabList[e.target.dataset.index].isSelect) {
      return false;
    }
    this.data.tabList[e.target.dataset.index].isSelect = true;
    this.setData({
      tabList: this.data.tabList,
      isSeclctMode: true,
      selectTotal: (++this.data.selectTotal)
    })
  },
  unselect: function(e) {
    if (!this.data.tabList[e.target.dataset.index].isSelect) {
      return false;
    }
    this.data.tabList[e.target.dataset.index].isSelect = false;
    this.setData({
      tabList: this.data.tabList,
      isSeclctMode: (--this.data.selectTotal > 0)
    })
  },
  formSubmit: function(e) {
    console.log(e)
    var selectTab = [];
    for (var item of this.data.tabList) {
      if (item.isSelect) {
        selectTab.push(item.id);
      }
    }
    if (selectTab.length > 0) {
      wx.navigateTo({
        url: '../map/map?id=' + selectTab.join(','),
      })
      return;
    }
    if (e.detail.value.keyword != '') {
      // this.getData(e.detail.value.keyword);
      wx.navigateTo({
        url: '../map/map?keyword=' + e.detail.value.keyword,
      })
    }
    wx.navigateTo({
      url: '../map/map',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})